package org.study.mongo.demo;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * 查询浏览量大于1000的记录
 * @author fangyu
 * @version v1.0.0
 * @date 2020/10/13 6:51 下午
 */
public class MongoDemo2 {
    public static void main(String[]args){
        // 创建连接
        MongoClient client = new MongoClient("10.211.55.6");
        // 打开数据库
        MongoDatabase spitdb = client.getDatabase("spitdb");
        // 获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");

        // 构建查询条件 => 查询浏览量大于1000的记录
        BasicDBObject bson = new BasicDBObject("visits", new BasicDBObject("$gt", 1000));

        // 查询记录获取文档集合
        FindIterable<Document> documents = spit.find(bson);

        for (Document document: documents) {
            System.out.println("内容:" + document.getString("content"));
            System.out.println("用户ID:" + document.getString("userid"));
            System.out.println("浏览量:" + document.getInteger("visits"));
        }
        // 关闭连接
        client.close();
    }
}
